home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4272 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Float calculations
  5. Date: 2 Feb 1996 17:24:38 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4eu2v6$bhj@umbc9.umbc.edu>
  8. References: <4eqssf$d9q@camelot.ccs.neu.edu>
  9. NNTP-Posting-Host: umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Jason Leatherman <jason@ccs.neu.edu> wrote:
  13. |>   Check out the results I get when running this simple program on a
  14. |> Sparc/UNIX system, compiled with gcc:
  15. |> 
  16. |> #include <stdio.h>
  17. |> 
  18. |> void main()
  19.  
  20. That alone is technically enough to produce undefined behavior in your
  21. program...Get rid of it! The main() function *HAS* to return an int.
  22.  
  23. |> {
  24. |>   float a, b;
  25.  
  26. Change this to double...
  27.  
  28. |>   printf("%0.10f  %0.10f  %0.10f\n", 99974.0, 50.0, 99974.0/50.0);
  29. |> 
  30. |>   a = 99974.0;
  31. |>   b = 50.0;
  32. |>   printf("%0.10f  %0.10f  %0.10f\n", a, b, a/b);
  33.  
  34. Add 'return (0);'...
  35. |> }
  36. |> 
  37. |> The output is:
  38. |> 99974.0000000000  50.0000000000  1999.4800000000
  39. |> 99974.0000000000  50.0000000000  1999.4799804688
  40. |> 
  41. |>   Why do the divisions produce different results?  This is probably some
  42. |> simple thing that I've forgotten, but I haven't figured it out yet.  Does
  43. |> anyone know?  Note that compiling with the -ffloat-store flag didn't make
  44. |> a difference.
  45.  
  46. Using doubles in one printf() and floats in the other resulted in a slight
  47. loss of accuracy. Using a floating point constant in printf will be treated
  48. as a double. Since a and b are float they get evaluated as such in the 
  49. division.
  50. -- 
  51. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  52.  
  53. Jonas J. Schlein  (schlein@gl.umbc.edu)
  54.